package com.fastCms.business.frame.utils.amp;

import com.alibaba.fastjson.JSON;
import com.fastCms.business.frame.utils.HttpclientUtils;
import com.fastCms.business.frame.utils.amp.response.WeatherInfoResp;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class AmapUtil {
    //天气信息
    private final static String WEATHER_INFO = "https://restapi.amap.com/v3/weather/weatherInfo?";

    public static WeatherInfoResp wechatherInfo(String key, String adcode, boolean isAll) {
        StringBuilder url = new StringBuilder(WEATHER_INFO);
        url.append("key=");
        url.append(key);
        url.append("&city=");
        url.append(adcode);
        if (isAll) {
            url.append("&extensions=all");
        } else {
            url.append("&extensions=base");
        }
        url.append("&output=JSON");
        try {
            log.info("请求的URL" + url);
            String response = HttpclientUtils.get(url.toString());
            log.info(response);
            if (response.contains("10000")) {
                WeatherInfoResp weatherInfoResp = JSON.parseObject(response, WeatherInfoResp.class);
                return weatherInfoResp;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        WeatherInfoResp weatherInfoResp = wechatherInfo("52b5907dbe248793c344e656741477f1", "371100", false);
    }
}
